package patrol.mobile.com.mobilepatrol.activity.yanghuke.zuoyeshangbao;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.activity.yanghuke.zuoyeshangbao.bean.ZuoYeBean;
import patrol.mobile.com.mobilepatrol.adapter.OtherContentAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.DateBean;
import patrol.mobile.com.mobilepatrol.bean.DictListBean;
import patrol.mobile.com.mobilepatrol.bean.OrgListBean;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.Constants;
import patrol.mobile.com.mobilepatrol.utils.DatePickerUtil;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.TimeUtils;
import patrol.mobile.com.mobilepatrol.utils.ToastUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;
import patrol.mobile.com.mobilepatrol.view.CallBack;
import patrol.mobile.com.mobilepatrol.widget.MySpinner;

import static patrol.mobile.com.mobilepatrol.utils.SPUtils.get;

/**
 * Created by z on 2018/9/13.
 * Des:作业详情页面
 */

public class ZuoyeDetailActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.mEditZyrq)
    TextView mEditZyrq;
    @BindView(R.id.mEditTqqk)
    MySpinner mEditTqqk;
    @BindView(R.id.mEditLux)
    EditText mEditLux;
    @BindView(R.id.mEditZhuangh)
    EditText mEditZhuangh;
    @BindView(R.id.mEditZhuangh02)
    EditText mEditZhuangh02;
    @BindView(R.id.mEditZhuanghend)
    EditText mEditZhuanghend;
    @BindView(R.id.mEditZhuanghend2)
    EditText mEditZhuanghend2;
    @BindView(R.id.mEditLeib)
    EditText mEditLeib;
    @BindView(R.id.mEditRens)
    EditText mEditRens;
    @BindView(R.id.mEditJsy)
    EditText mEditJsy;
    @BindView(R.id.mEditAqy)
    EditText mEditAqy;
    @BindView(R.id.mEditcontent)
    EditText mEditcontent;
    @BindView(R.id.mEditDaoban)
    EditText mEditDaoban;
    @BindView(R.id.zuoyeDetailButton1)
    TextView zuoyeDetailButton1;
    @BindView(R.id.zuoyeDetailButton2)
    TextView zuoyeDetailButton2;
    @BindView(R.id.zuoyeDetailButton3)
    TextView zuoyeDetailButton3;
    @BindView(R.id.riqiXuanzeQiLayout)
    LinearLayout riqiXuanzeQiLayout;
    @BindView(R.id.sp_type)
    MySpinner spType;
    @BindView(R.id.sp_Daoban)
    MySpinner spDaoban;
    //
    private PopupWindow mPopWindow;
    private View contentView;
    private String enId;
    private String orgId;
    private TextView[] editTexts;
    private ZuoYeBean zuoYeBean;
    private ZuoYeBean zuoYeNewBeanBean;
    private List<String> tianqi;
    private String tianqiText = "晴";
    private String mainId;
    private List<String> largelist = new ArrayList<>();

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_zuoye_detail_ayout;
    }

    @Override
    public void initTitle() {
        super.initTitle();
        contentView = LayoutInflater.from(this).inflate(R.layout.pop_content, null);
        mPopWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("作业信息");
        enId = getIntent().getStringExtra("zuoyeListBean");
        editTexts = new TextView[]{mEditZyrq, mEditLux, mEditZhuangh, mEditZhuangh02, mEditZhuanghend, mEditZhuanghend2, mEditcontent, mEditRens, mEditJsy, mEditAqy};
        if (!TextUtils.isEmpty(enId)) {
            for (TextView editText : editTexts) {
                editText.setEnabled(false);
                editText.setTextColor(getResources().getColor(R.color.text2));
            }
            setEditText();
            tv_right_title.setVisibility(View.VISIBLE);
            tv_right_title.setText("提交");
        } else {
            mainId = System.currentTimeMillis() + "";
            for (TextView editText : editTexts) {
                editText.setEnabled(true);
            }
            tv_right_title.setVisibility(View.VISIBLE);
            tv_right_title.setText("提交");
            tv_right_title.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    commitHttp();
                }
            });
        }
        getDictList("worksType");
        getStateList();
        mEditZyrq.setText(TimeUtils.timeString(System.currentTimeMillis()));
        tianqi = new ArrayList<>();
        tianqi.add("晴");
        tianqi.add("多云");
        tianqi.add("小雨");
        tianqi.add("小雪");
        mEditTqqk.setmDatas(tianqi, new MySpinner.CallBack() {
            @Override
            public void onSelect(int position) {
                tianqiText = tianqi.get(position);
            }
        });
//        mEditcontent.setOnFocusChangeListener(new View.OnFocusChangeListener() {
//            @Override
//            public void onFocusChange(View v, boolean hasFocus) {
//                if (hasFocus) {
//                    mPopWindow.showAsDropDown(v);
//                }
//            }
//        });
//        mEditcontent.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mPopWindow.showAsDropDown(v);
//            }
//        });
    }

    private void getDictList(final String dictType) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.DICT_LIST);
        HashMap<String, String> map = new HashMap<>();
        map.put("dictType", dictType);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();
                JsonArray listArray = jo.getAsJsonArray("list");
                List<DictListBean> dictListBeanList = GsonUtil.jsonToList(listArray.toString(), DictListBean.class);
//                spType
                for (int i = 0; i < dictListBeanList.size(); i++) {
                    largelist.add(dictListBeanList.get(i).getLabel());
                }
                spType.setmDatas(largelist, new MySpinner.CallBack() {
                    @Override
                    public void onSelect(int postion) {
                        spType.setText(largelist.get(postion));
                    }
                });
//                showPopupWindow(dictListBeanList);
            }
        });
    }

    private void getStateList() {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getOrgList);
        HashMap<String, String> map = new HashMap<>();
        map.put("objType", "养护站");
        map.put("orgId", UserManager.getOrgNo());
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                final OrgListBean orgListBean = new Gson().fromJson(response, OrgListBean.class);
                List<String> list = new ArrayList<>();
                for (OrgListBean.ListBean listBean : orgListBean.getList()) {
                    list.add(listBean.getOrgName());
                }
                spDaoban.setmDatas(list, new MySpinner.CallBack() {
                    @Override
                    public void onSelect(int postion) {
                        orgId = orgListBean.getList().get(postion).getId();
                    }
                });
            }
        });
    }

    private void showPopupWindow(List<DictListBean> dictListBeanList) {
        //设置contentView

        mPopWindow.setContentView(contentView);
        //设置各个控件的点击响应
        RecyclerView recyclerView = contentView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        OtherContentAdapter otherContentAdapter = new OtherContentAdapter(this);
        otherContentAdapter.addItems(dictListBeanList);
        recyclerView.setAdapter(otherContentAdapter);
        otherContentAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                mEditcontent.setText(otherContentAdapter.getData().get(position).getValue());
                mPopWindow.dismiss();
            }
        });
        //显示PopupWindow

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void setEditText() {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("id", enId);
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getYhkOperationAreaEntity);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                zuoYeBean = new Gson().fromJson(response, ZuoYeBean.class);
                mEditZyrq.setText(zuoYeBean.getWorksDate());
                mEditTqqk.setText(zuoYeBean.getWeather());
                mEditLux.setText(zuoYeBean.getRoutelinename());
                if (zuoYeBean.getPot().contains("-")) {
                    String[] split = zuoYeBean.getPot().split("-");
                    mEditZhuangh.setText(split[0].split("\\+")[0]);
                    mEditZhuanghend.setText(split[0].split("\\+")[1]);
                    mEditZhuangh02.setText(split[1].split("\\+")[0]);
                    mEditZhuanghend2.setText(split[1].split("\\+")[1]);
                } else {
                    mEditZhuangh.setText(zuoYeBean.getPot());
                }

//                mEditLeib.setText(zuoYeBean.getWorksType());
                spType.setText(zuoYeBean.getWorksType());
                mEditRens.setText(zuoYeBean.getWorksNum());
                mEditJsy.setText(zuoYeBean.getTechnician());
                mEditAqy.setText(zuoYeBean.getSecurity());
//                mEditDaoban.setText(zuoYeBean.getWorksTeam());
                spDaoban.setText(zuoYeBean.getWorksTeam());
                mEditcontent.setText(zuoYeBean.getWorksInfo());
                zuoyeDetailButton1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (zuoYeBean.getYhkOperationAreaPostList() != null && zuoYeBean.getYhkOperationAreaPostList().size() > 0) {
                            startActivity(new Intent(v.getContext(), GqpxActivity.class)
                                    .putExtra("id", zuoYeBean.getId())
                                    .putExtra("yhkOperationAreaPostListBean", zuoYeBean.getYhkOperationAreaPostList().get(0)));
                        } else {
                            startActivity(new Intent(v.getContext(), GqpxActivity.class)
                                    .putExtra("id", zuoYeBean.getId()));
                        }
                    }
                });
                zuoyeDetailButton2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (zuoYeBean.getYhkOperationAreaLayList() != null && zuoYeBean.getYhkOperationAreaLayList().size() > 0) {
                            startActivity(new Intent(v.getContext(), XcaqBfActivity.class)
                                    .putExtra("id", zuoYeBean.getId())
                                    .putExtra("yhkOperationAreaLayListBean", zuoYeBean.getYhkOperationAreaLayList().get(0)));
                        } else {
                            startActivity(new Intent(v.getContext(), XcaqBfActivity.class)
                                    .putExtra("id", zuoYeBean.getId()));
                        }
                    }
                });
                zuoyeDetailButton3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(v.getContext(), ZuoyeWanchengGzLActivity.class)
                                .putExtra("id", zuoYeBean.getId()));
                    }
                });
            }
        });
    }

    @Override
    public void setListener() {
        riqiXuanzeQiLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerUtil.getInstance(v.getContext()).setCallBack(new CallBack<DateBean>() {
                    @Override
                    public void onBack(DateBean dateBean) {
                        mEditZyrq.setText(dateBean.getmDate());
                    }
                });
            }
        });
        zuoyeDetailButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(enId)){
                    ToastUtils.showToast("请先提交作业信息");
                   return;
                }
                startActivity(new Intent(v.getContext(), GqpxActivity.class)
                        .putExtra("id", mainId)
                );

            }
        });
        zuoyeDetailButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(enId)){
                    ToastUtils.showToast("请先提交作业信息");
                    return;
                }
                startActivity(new Intent(v.getContext(), XcaqBfActivity.class)
                        .putExtra("id", mainId)
                );
            }
        });
        zuoyeDetailButton3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(enId)){
                    ToastUtils.showToast("请先提交作业信息");
                    return;
                }
                startActivity(new Intent(v.getContext(), ZuoyeWanchengGzLActivity.class));
            }
        });
    }

    private void commitHttp() {
        for (TextView editText : editTexts) {
            if (TextUtils.isEmpty(editText.getText())) {
                ToastUtils.showToast("请填写完整信息");
                return;
            }
        }
        zuoYeBean = new ZuoYeBean();
        zuoYeBean.setId(mainId);
        zuoYeBean.setWorksDate(mEditZyrq.getText().toString());
        zuoYeBean.setWeather(mEditTqqk.getmText().getText().toString());
        zuoYeBean.setRoutelinename(mEditLux.getText().toString());
        zuoYeBean.setPot("K" + mEditZhuangh.getText().toString() + "+" + mEditZhuangh02.getText().toString() + "-" + "K" + mEditZhuanghend.getText().toString() + "+" + mEditZhuanghend2.getText().toString());
        zuoYeBean.setWorksType(spType.getmText().getText().toString());
        zuoYeBean.setWorksNum(mEditRens.getText().toString());
        zuoYeBean.setTechnician(mEditJsy.getText().toString());
        zuoYeBean.setSecurity(mEditAqy.getText().toString());
        zuoYeBean.setWorksTeam(spDaoban.getmText().getText().toString());
        zuoYeBean.setWorksInfo(mEditcontent.getText().toString());
        zuoYeBean.setOrgId(UserManager.getOrgNo());
        zuoYeBean.setOrgName(UserManager.getUser((String) get(Constants.current_id, "")).bmname);
        ZuoYeBean.MainID mainID = new ZuoYeBean.MainID();
        mainID.setId(mainId);
        zuoYeBean.setMainId(mainID);
        HashMap<String, String> shitiHashMap = new HashMap<>();
        shitiHashMap.put("mainJson", new Gson().toJson(zuoYeBean));
        HttpSender sender = new HttpSender(this, baseUrl, Urls.yhkOperationAreaUploadData);
        sender.requestPost(shitiHashMap, new HttpCallBack(sender) {

            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                Log.e("test", response);
                enId = mainId;
                ToastUtils.showToast("提交成功");
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        }
    }
}
